/**************************************************************************
 * @name: A.c
 * @brief: 模拟客户端
 * @date: 2025/02/10
 * @author: boluzs@163.com
 * @note:
 *  1.硬件初始化
 *  2.输账号密码
 *  3.发送验证码
 * @version: 1.0.0
 * @Copyright (c) 2025 by boluzs@163.com, All Rights Reserved.
 **************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#define FIFO_PATH "/tmp/abfifo"

int main(int argc, char const *argv[])
{
    // 打开有名管道
    int fifo_fd = open(FIFO_PATH, O_RDWR);
    if (fifo_fd == -1)
    {
        fprintf(stderr, "open [%s] error,%s\n", FIFO_PATH, strerror(errno));
        exit(-1);
    }
    while (1)
    {
        // 1.硬件初始化
        // 2.输账号密码
        // 3.发送验证码(发送信号，请求验证码)
        // 4.定义B进程可以传的字符串
        int n = 0;
        int pid = 0;
        while (n != 1)
        {
            // system("ps aux");
            printf("请输入一个B进程的pid\n");
            n = scanf("%d", &pid);
            kill(pid, SIGUSR1);
            if (n != 1)
                printf("输入的数据类型有误, 请重新输入\n");
            while (getchar() != '\n')
                ;
        }
        n = 0;
        char code[5] = {0};
        while (n != 1)
        {
            printf("请输入验证码\n");
            n = scanf("%s", code);
            if (n != 1)
                printf("输入的数据类型有误, 请重新输入\n");
            while (getchar() != '\n')
                ;
        }
        n = 0;

        // 6.写入到有名管道
        write(fifo_fd, code, sizeof(code));

        // 7.给进程B发信号
        kill(pid, SIGUSR2);
    }

    return 0;
}
